<?php
/**
* This file belongs to the core webappkit package (kit)
* @link http://webappkit.net
* @package webappkit
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/


if (class_exists('webappkit'))
	return;

/**
* webappkit "kit" manipulation class.
* This is the very core class for webappkit packaging system.
*/
class webappkit {
/*--------------------------------------------------------------------
 STATIC METHODS
--------------------------------------------------------------------*/
	
	/**
	* factory : use this to get a kit instance.
	* @param string $id kit id : use / to access subkits
	* @static
	* @return webappkit
	*/
	public static function & getKit($id) {
		// storing instances in a static array
		static $kits=array();
		if (!isset($kits[$id])) {
			// trying to build kit instance
			$path=webappkit::getPath($id);
			// reading meta
			$meta=webappkit::getMeta($id);
			// loading external libraries (kits dependancies)
			foreach ($meta['load_kits'] as $subkit)
				webappkit::getkit($subkit);
			// loading internal libraries
			foreach ($meta['load_files'] as $file)
				webappkit::loadLibFile($path.$file);
			// defining kit's class (default is webappkit)
			// defing kit's inheritance (tools ?)
			// building instance
			$kits[$id]=&new webappkit($id,$meta);
		}
		return $kits[$id];
	}
	
	/**
	* returns spent time in microseconds, with chosen precision
	* if not defined, defines WAK_START_TIME as reference microtime
	* @param int $precision (default 5) 
	* @static
	* @access public
	* @return float spent time since the first call
	*/
	public static function timeCounter($precision=5) {
		list($usec, $sec) = explode(" ", microtime());
		$microtime=((float)$usec + (float)$sec);
		if (!defined('WAK_START_TIME'))
			define('WAK_START_TIME',$microtime);
		//echo $microtime;
		$timeCounter=$microtime-WAK_START_TIME;
		return round($timeCounter,$precision);
	}

	/**
	* checks/cleans kit ID
	* @static
	* @access public
	* @param string
	* @return string or false if id is invalid
	*/
	public static function checkID($id) {
		$id=strtolower($id);
		if (!ereg('^[a-z][a-z0-9_]*(/[a-z][a-z0-9_]*)*$',$id))
			return false;
		return $id;
	}

	/**
	* get kit path from kit id
	* @static
	* @access public
	* @param string $id
	* @return string path with trailing slash
	*/
	public static function getPath($id) {
		$levels=explode('/',$id);
		$path='';
		foreach ($levels as $l)
			$path.="$l.wak/";
		$path=WAK_ROOT_PATH.$path;
		if (!file_exists($path)) {
			trigger_error("invalid path {$path} for id {$id}");
			return false;
		}
		return $path;
	}
	
	
	/**
	* loads package/kit metadata from ini file
	* @access public
	* @param string $id
	* @return array
	* @todo checking content format
	*/
	public static function getMeta($id) {
		// path
		$path=webappkit::getpath($id);
		// getting ini file
		$file=$path.'wak.ini';
		if (!file_exists($file) or !is_file($file)) {
			trigger_error("kit metadata file $file not found");
			return false;
		}
			//throw new Exception("package info file $file not found");
		// parsing
		if (!$meta=parse_ini_file($file,true)) {
			//throw new Exception("failed to parse $file");
			trigger_error("failed to parse $file");
			return false;
		}
		// correcting array format if necessary
		if (!isset($meta['load_kits']))
			$meta['load_kits']=array();
		if (!isset($meta['load_files']))
			$meta['load_files']=array();
		// echo '<pre>'; print_r($meta);
		//return $this->meta?true:false;
		// $this->loaded_files[]=$file;
		return $meta;
	}
	
	/**
	 * loads a library file
	 */
	public static function loadLibFile($file) {
		// auto php version ?
		if (substr($file,-1,1)=='*') {
			$php=substr(phpversion(),0,1);
			$file=substr($file,0,-1).$php;
		}
		// checking existence
		if (!file_exists($file)) {
			trigger_error("can't include file $file, don't exist");
			return false;
		}
		include($file); //echo "<p>included $file</p>";
		return true;
	}
	
	public function __construct($id,$meta) {
		$this->id=$id;
		$this->meta=&$meta;
	}
}
?>